package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.receive.ReceiveSelectReceiverActivity;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOperatorBean;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class OtherSelectOperatorListAdapter extends BaseAdapter {

    private Context mContext;
    private List<InStorageOperatorBean.DataBean.ReceiverListBean> mSelectReceiverList;
    private LayoutInflater inflater;
    private ReceiveSelectReceiverActivity.ReceiveSelectReceiverListener mReceiveSelectReceiverListener;

    public OtherSelectOperatorListAdapter(Context context, List<InStorageOperatorBean.DataBean.ReceiverListBean> selectReceiverList, ReceiveSelectReceiverActivity.ReceiveSelectReceiverListener receiveSelectReceiverListener) {
        mContext = context;
        mSelectReceiverList = selectReceiverList;
        mReceiveSelectReceiverListener = receiveSelectReceiverListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mSelectReceiverList.size();
    }

    @Override
    public Object getItem(int position) {
        return mSelectReceiverList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        InStorageOperatorBean.DataBean.ReceiverListBean receiverItem = mSelectReceiverList.get(position);
        ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_in_storage_select_receiver_list, null);
            viewHolder = new ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, receiverItem);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.id = (TextView) view.findViewById(R.id.id);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.userName = (TextView) view.findViewById(R.id.userName);
        viewHolder.company = (TextView) view.findViewById(R.id.company);
        viewHolder.department = (TextView) view.findViewById(R.id.department);
        viewHolder.roleName = (TextView) view.findViewById(R.id.roleName);
        viewHolder.roleCode = (TextView) view.findViewById(R.id.roleCode);
        viewHolder.phone = (TextView) view.findViewById(R.id.phone);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InStorageOperatorBean.DataBean.ReceiverListBean receiverItem) {
        viewHolder.id.setText(receiverItem.getId());
        viewHolder.name.setText(receiverItem.getName());
        viewHolder.userName.setText(receiverItem.getUserName());
        viewHolder.company.setText(receiverItem.getCompany());
        viewHolder.department.setText(receiverItem.getDepartment());
        viewHolder.roleName.setText(receiverItem.getRoleName());
        viewHolder.roleCode.setText(receiverItem.getRoleCode());
        viewHolder.phone.setText(receiverItem.getPhone());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                GetOperatorBean.OperatorBean receiveReceiver = new GetOperatorBean.OperatorBean();
                receiveReceiver.setUser_id(receiverItem.getId());
                receiveReceiver.setUser_name(receiverItem.getName());
                receiveReceiver.setOrg_name(receiverItem.getDepartment());
                receiveReceiver.setRole_name(receiverItem.getRoleName());
                receiveReceiver.setRole_id(receiverItem.getRoleCode());
                receiveReceiver.setMobile(receiverItem.getPhone());
                InStorageUtils.addOtherOperator(context, receiveReceiver);
                mReceiveSelectReceiverListener.onSelectReceiver();
            }
        });
    }

    private class ViewHolder {
        private TextView id, name, userName, company, department, roleName, roleCode, phone;
    }

}
